package com.baidu.erp.web;

import com.alibaba.fastjson.JSONObject;
import com.baidu.erp.domain.Permission;
import com.baidu.erp.sercvice.PermissionService;
import com.baidu.erp.sercvice.impl.PermissionServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@WebServlet("/permission")
public class PermissionServlet extends BaseServlet{
    /**
     * 跳转到主页
     */
    public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/qx/permission/index.jsp").forward(request,response);
    }
    //跳转到tree

    /**
     * 跳转到 tree
     */
    public void tree(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/qx/permission/tree.jsp").forward(request,response);

    }
    /**
     * 跳转到workarea
     */
    public void workarea(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/qx/permission/workarea.jsp").forward(request,response);
    }

    /**
     * 跳转到 详情 页面
     */
    public void  detail(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        getPermissionById(request,response);
        request.getRequestDispatcher("WEB-INF/qx/permission/detail.jsp").forward(request,response);
    }

    /**
     * 获取权限列表
     */
    public void getPermissionList(HttpServletRequest request,HttpServletResponse response) throws IOException {
        PermissionService permissionService = new PermissionServiceImpl();
        List<Permission> permissionList = permissionService.getAll();
        String jsonString = JSONObject.toJSONString(permissionList);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().println(jsonString);
    }

    /**
     * 根据id查询权限信息
     */
    public void getPermissionById(HttpServletRequest request,HttpServletResponse response){
        PermissionService permissionService = new PermissionServiceImpl();
        String id = request.getParameter("id");
        Permission permission = permissionService.getPermissionById(id);//执行语句获取实体类对象
        request.setAttribute("permission",permission);
    }

    /**
     * 跳转到add页面
     */
    public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String pId = request.getParameter("id");
        request.setAttribute("pId",pId);
        request.getRequestDispatcher("WEB-INF/qx/permission/add.jsp").forward(request,response);
    }

    /**
     * 跳转到edit页面
     */
    public void edit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        PermissionService permissionService = new PermissionServiceImpl();
        Permission permission = permissionService.getPermissionById(id);
        request.setAttribute("permission",permission);
        request.getRequestDispatcher("WEB-INF/qx/permission/edit.jsp").forward(request,response);
    }

    /**
     * 新增权限到数据库
     */
    public void save(HttpServletRequest request,HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        response.setContentType("text/json;charset=UTF-8");
        //获取表单提交的数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //建立对象
        Permission permission = new Permission();
        //使用工具将对象map的数据拷贝到对象permission中
        BeanUtils.populate(permission,parameterMap);
        PermissionService permissionService = new PermissionServiceImpl();
        int count = permissionService.save(permission);

        HashMap<String, String> jsonMap = new HashMap<>();
    if (count>0){
            jsonMap.put("code","200");
            jsonMap.put("mesg","保存成功");
            jsonMap.put("id",permission.getId());
        }else {
            jsonMap.put("code","500");
            jsonMap.put("mesg","保存失败");
        }
        response.getWriter().println(JSONObject.toJSONString(jsonMap));
    }

    /**
     * 更新权限
     */
    public void update(HttpServletRequest request,HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        response.setContentType("text/json;charset=UTF-8");
        //获取表单提交的数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //建立对象
        Permission permission = new Permission();
        //使用工具将对象map的数据拷贝到对象permission中
        BeanUtils.populate(permission,parameterMap);
        PermissionService permissionService = new PermissionServiceImpl();
        int count = permissionService.update(permission);

        HashMap<String, String> jsonMap = new HashMap<>();
        if (count>0){
            jsonMap.put("code","200");
            jsonMap.put("mesg","更新成功");
            jsonMap.put("id",permission.getId());
        }else {
            jsonMap.put("code","500");
            jsonMap.put("mesg","更新失败");
        }
        response.getWriter().println(JSONObject.toJSONString(jsonMap));
    }

    /**
     * 删除权限
     */
    public void del(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=UTF-8");
        String id = request.getParameter("id");
        PermissionService permissionService = new PermissionServiceImpl();
        permissionService.getSonPermissionById(id);
        int count = permissionService.delete(id);
        HashMap<String, String> jsonMap = new HashMap<>();
        if (count>0){
            jsonMap.put("code","200");
            jsonMap.put("mesg","删除成功");
        }else {
            jsonMap.put("code","500");
            jsonMap.put("mesg","删除失败");
        }
        response.getWriter().println(JSONObject.toJSONString(jsonMap));
    }

    /**
     * 根据pId和name查询权限
     */
    public void getPermissionNameAndPid(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=UTF-8");
        String pId = request.getParameter("pId");
        String name = request.getParameter("name");
        PermissionService permissionService = new PermissionServiceImpl();
        Optional<Permission> optionalPermission = permissionService.getPermissionNameAndPid(pId,name);
        HashMap<String, String> jsonMap = new HashMap<>();
        //默认为200，即该用户不存在
        jsonMap.put("code","200");
        //数据库中有重复的 pId和name 修改code为500，即该用户存在
        optionalPermission.ifPresent(e->jsonMap.put("code","500"));
        response.getWriter().println(JSONObject.toJSONString(jsonMap));
    }

}
